package zgrjb.snbq.teach_platform.controller;

import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import zgrjb.snbq.teach_platform.dto.model.User;
import zgrjb.snbq.teach_platform.interceptor.LoginInterceptor;
import zgrjb.snbq.teach_platform.service.NotificationService;
import zgrjb.snbq.teach_platform.util.JsonData;

/**
 * 通知管理控制器
 * 
 * @author system
 * @since 2024-12-28
 */
@RestController
@RequestMapping("/api/v1/notification")
@Slf4j
public class NotificationController {

    @Autowired
    private NotificationService notificationService;

    /**
     * 获取通知列表
     */
    @GetMapping("/list")
    public JsonData getNotifications(@RequestParam(defaultValue = "1") int page,
                                   @RequestParam(defaultValue = "10") int size,
                                   @RequestParam(required = false) String type) {
        User user = LoginInterceptor.getCurrentUser();
        return notificationService.getUserNotifications(user.getId(), page, size, type);
    }

    /**
     * 标记通知已读
     */
    @PostMapping("/read/{id}")
    public JsonData markAsRead(@PathVariable Long id) {
        User user = LoginInterceptor.getCurrentUser();
        return notificationService.markNotificationAsRead(id, user.getId());
    }

    /**
     * 批量标记已读
     */
    @PostMapping("/read-all")
    public JsonData markAllAsRead() {
        User user = LoginInterceptor.getCurrentUser();
        return notificationService.markAllAsRead(user.getId());
    }

    /**
     * 获取未读通知数量
     */
    @GetMapping("/unread-count")
    public JsonData getUnreadCount() {
        User user = LoginInterceptor.getCurrentUser();
        return notificationService.getUnreadNotificationCount(user.getId());
    }

    /**
     * 删除通知
     */
    @DeleteMapping("/{id}")
    public JsonData deleteNotification(@PathVariable Long id) {
        User user = LoginInterceptor.getCurrentUser();
        return notificationService.deleteNotification(id, user.getId());
    }

    /**
     * 生成AI建议
     */
    @PostMapping("/ai-suggestion/{id}")
    public JsonData generateAISuggestion(@PathVariable Long id) {
        return notificationService.generateAISuggestion(id);
    }

    /**
     * 创建系统通知（管理员功能）
     */
    @PostMapping("/system")
    public JsonData createSystemNotification(@RequestBody CreateNotificationRequest request) {
        // 这里可以添加权限验证，确保只有管理员可以创建系统通知
        return notificationService.createSystemNotification(
            request.getUserId(),
            request.getTitle(),
            request.getContent(),
            request.getPriority()
        );
    }

    /**
     * 创建通知请求体
     */
    public static class CreateNotificationRequest {
        private Long userId;
        private String title;
        private String content;
        private Integer priority;

        // Getters and Setters
        public Long getUserId() { return userId; }
        public void setUserId(Long userId) { this.userId = userId; }
        
        public String getTitle() { return title; }
        public void setTitle(String title) { this.title = title; }
        
        public String getContent() { return content; }
        public void setContent(String content) { this.content = content; }
        
        public Integer getPriority() { return priority; }
        public void setPriority(Integer priority) { this.priority = priority; }
    }
} 